package com.ex3.ylw.connector.http;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 一个连接器，
 * 1.实现了Runable接口
 * 2.声明协议scheme是http
 * 3.创建一个Socket接口，用来接收Socket，并把连接器和接收的socket传递给HttpProcessor,并调用process方法
 * Created by yangliwei on 2017/7/25.
 */
public class HttpConnector implements Runnable {
    private final String scheme = "http";
    private boolean stop;

    /**
     * 接受socket病传参给HttpProcess
     */
    @Override
    public void run() {
        ServerSocket server = null;
        try {
            server = new ServerSocket(8080, 1, InetAddress.getByName("127.0.0.1"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        while (!stop) {
            Socket socket = null;
            try {
                socket = server.accept();

            } catch (IOException e) {
//                出现异常继续
                continue;
            }
            //这里传的是连接器，然后再把socket传给process方法，
//            HttpProcessor httpProcessor = new HttpProcessor(this);
//            httpProcessor.process(socket);
        }

    }

    /**
     * 启动该连接器
     */
    public void start(){
        Thread thread = new Thread(this);
        thread.start();
    }

    public String getScheme() {
        return scheme;
    }
}
